package com.example.starapp.starfrag;
import android.annotation.SuppressLint;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.animee.starapp.starfrag.StarInfo;

import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.starapp.R;
import com.example.starapp.StarInfoActivity;


import java.util.ArrayList;
import java.util.List;

/**
 * 星座Fragment
 * 包括 ViewPager 和  GridView
 */
public class StarFragment extends Fragment {
    ViewPager starVp;
    GridView starGv;
    LinearLayout pointLayout;
    private StarBaseAdapter adapter;
    //声明图片数组
    int[]imgIds={R.mipmap.pic_guanggao,R.mipmap.pic_star};
    List<ImageView>ivList;
    List<ImageView>pointList;
    private StarPagerAdapter starPagerAdapter;
    @SuppressLint("HandlerLeak")
    Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what==1) {
                int currentItem = starVp.getCurrentItem();
                if (currentItem==ivList.size()-1) {
                    //判断是否为最后一张
                    starVp.setCurrentItem(0);
                }else {
                    currentItem++;
                    starVp.setCurrentItem(currentItem);
                }
                handler.sendEmptyMessageDelayed(1,5000);
            }
        }
    };
    public StarFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_star, container, false);
        //查找控件
        starGv=view.findViewById(R.id.starfrag_gv);
        starVp=view.findViewById(R.id.starfrag_vp);
        pointLayout=view.findViewById(R.id.starfrag_layout);
        initPager();
        initGrid();
        setVpListener();
        //发送消息通知handler切换页面
        handler.sendEmptyMessageDelayed(1,5000);
        return view;
    }



    //生成九宫格当中的数据
    /*生成九宫格当中的数据*/
    private void initGrid() {
//      获取传递过来的数据
        Bundle bundle=getArguments();
        StarInfo starInfo= (StarInfo) bundle.getSerializable("info");
        List<StarInfo.StarinfoBean> list = starInfo.getStarinfo();  //数据源
//        创建适配器
        adapter = new StarBaseAdapter(getContext(), list);
//        设置适配器
        starGv.setAdapter(adapter);
        starGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), StarInfoActivity.class);
                startActivity(intent);
            }
        });
    }

    //初始化ViewPager的界面的函数
    private void initPager() {
        //声明数据源集合
        ivList=new ArrayList<>();
        pointList=new ArrayList<>();
        for (int i = 0; i <imgIds.length ; i++) {
            ImageView iv=new ImageView(getContext());
            iv.setImageResource(imgIds[i]);//设置现实的图片
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//显示样式
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(lp);//宽高
            ivList.add(iv);
            ImageView pointIv=new ImageView(getContext());
            pointIv.setImageResource(R.mipmap.point_normal);
            LinearLayout.LayoutParams plp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            plp.setMargins(20,0,0,0);
            pointIv.setLayoutParams(plp);
            //添加到布局
            pointLayout.addView(pointIv);
            pointList.add(pointIv);
        }
        pointList.get(0).setImageResource(R.mipmap.point_focus);//默认第一个显示为选中
        starPagerAdapter = new StarPagerAdapter(getContext(), ivList);
        starVp.setAdapter(starPagerAdapter);
    }
    private void setVpListener() {
        starVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0; i < pointList.size(); i++) {
                    pointList.get(i).setImageResource(R.mipmap.point_normal);
                }
                pointList.get(position).setImageResource(R.mipmap.point_focus);
            }
        });
    }
}